package com.ssm.promotion.core.mydlbp_nsgaiii.core;

import com.ssm.promotion.core.mydlbp_nsgaiii.util.AlgLogger;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Create by whk0710 on 2/18/2019
 * 此类用于保存每次迭代后Solution集合，可以用于下一次迭代的初始种群
 * 参考自jMetal与Yuan yuan FJSP问题代码
 * */
public class SolutionSet implements java.io.Serializable{

    //种群集合
    protected List<Solution> solution_list;

    //种群大小
    private int capacity;

    //默认构造器
    public SolutionSet(){
        solution_list = new ArrayList<Solution>(); //线性表保存每个个体
    }

    //指定容量
    public SolutionSet(int max_pop){
        this();
        capacity = max_pop;
    }

    //向种群集合中添加个体
    public boolean add(Solution solution){
        //容量超过指定大小
        if(solution_list.size() == capacity){
            AlgLogger.logger.severe("The population is full");
            AlgLogger.logger.severe("Capacity is : " + capacity);
            AlgLogger.logger.severe("\\t Size is: " + this.size());
            return false;
        }

        solution_list.add(solution);
        return true;
    }

    //按下标读取个体
    public Solution get(int i){
        if(i >= solution_list.size()) throw new IndexOutOfBoundsException("Index out of Bound " + i);
        return solution_list.get(i);
    }

    //读取容量
    public int getMaxSize(){
        return this.capacity;
    }

    //读取当前大小
    public int size(){
        return solution_list.size();
    }

    //清除列表
    public void clear(){
        solution_list.clear();
    }

    //迭代器
    public Iterator<Solution> iterator(){
        return solution_list.iterator();
    }

    //将种群中每个个体的目标函数值打印至一个二维数组中
    public double[][] writeObjectivesToMatrix(){
        if (this.size() == 0) {
            return null;
        }
        //创建数组
        double[][] objs = new double[size()][get(0).numberOfObjectives()];

        for(int i = 0;i < size();i++){
            for(int j = 0;j < get(0).numberOfObjectives();j++){
                objs[i][j] = get(i).getObjective(j);
            }
        }

        return objs;
    }



    //获取F1层个体
    public SolutionSet getNonDominatedSolutions(SolutionSet population){
        int size = population.size();


        return null;
    }
}
